<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="资讯名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入资讯名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资讯栏目" prop="subjectId">
         <treeselect :default-expand-level="1" v-model="queryParams.subjectId" @change="handleQuery" :options="subjectOptions" :normalizer="normalizer" placeholder="选择上级栏目" style="width:200px"/>
      </el-form-item>
      <el-form-item label="咨询状态" prop="statusOpt">
        <el-select v-model="queryParams.statusOpt" placeholder="请选择咨询状态" clearable size="small">
          <el-option
            v-for="dict in statusOptOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['consultation:content:edit']"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="contentList" >
      <el-table-column label="编号" align="center" prop="consultationId" />
      <el-table-column label="资讯名称" align="center" prop="title" />
      <el-table-column label="资讯栏目" align="center" prop="subjectName" />
      <el-table-column label="创建人员" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
      </el-table-column>
      <el-table-column label="咨询状态" align="center" prop="statusOpt" :formatter="statusOptFormat" />
      <el-table-column label="发布状态" align="center" prop="publishStatusOpt" :formatter="publishStatusOptFormat" />
      <el-table-column label="显示顺序" align="center" prop="sortNo" />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetails(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['consultation:content:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['consultation:content:edit']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信息咨询对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入资讯标题" />
        </el-form-item>
        <el-form-item label="栏目" prop="subjectId">
          <treeselect v-model="form.subjectId" :options="subjectOptions" :normalizer="normalizer" placeholder="选择上级栏目"/>
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="statusOpt">
              <el-select v-model="form.statusOpt" placeholder="请选择状态" style="width:100%">
                <el-option
                  v-for="dict in statusOptOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示顺序" prop="sortNo">
              <el-input v-model="form.sortNo" placeholder="请输入显示顺序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="附件" >
          <UploadCom ref="upload" @submitData="submitData"  description="" :userFileList="fileList" class="form_long"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" 
        v-hasPermi="['consultation:content:edit']">保存</el-button>
        <el-button type="primary" @click="publishForm" 
        v-hasPermi="['consultation:content:edit']">发布</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <ConsultDetail ref="detail" title="信息咨询查看"/>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent, exportContent } from "@/api/consultation/content";
import Editor from '@/components/Editor';
import { listSubject,} from "@/api/consultation/subject";
import ConsultDetail from "./details.vue"
import Treeselect from "@riophae/vue-treeselect";
import UploadCom from "@/views/labdata/commons/upload.vue";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Content",
  components: { Editor,Treeselect,UploadCom,ConsultDetail },
  data() {
    return {
      //附件集合
      fileList: [],
      // 部门树选项
      subjectOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息咨询表格数据
      contentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 咨询状态字典
      statusOptOptions: [],
      // 发布状态
      publishStatusOptions:[],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        subjectId: null,
        statusOpt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        subjectId: [
          { required: true, message: "栏目不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        statusOpt: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("consultationStatusOpt").then(response => {
      this.statusOptOptions = response.data;
    });
    this.getDicts("publishStatusOpt").then(response => {
      this.publishStatusOptions = response.data;
    });
    listSubject().then(response => {
      this.subjectOptions = this.handleTree(response.data, "subjectId");
    });
  },
  methods: {
    /** 查询信息咨询列表 */
    getList() {
      this.loading = true;
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleDetails(row){
      this.$refs["detail"].openDetail(row.consultationId);
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.subjectId,
        label: node.subjectName,
        children: node.children
      };
    },
    // 咨询状态字典翻译
    statusOptFormat(row, column) {
      return this.selectDictLabel(this.statusOptOptions, row.statusOpt);
    },
    // 咨询状态字典翻译
    publishStatusOptFormat(row, column) {
      return this.selectDictLabel(this.publishStatusOptions, row.publishStatusOpt);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        statusOpt: 1,
      };
      this.fileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.consultationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信息咨询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const consultationId = row.consultationId || this.ids
      getContent(consultationId).then(response => {
        this.form = response.data;
        this.fileList = this.form.images;
        this.open = true;
        this.title = "修改信息咨询";
      });
    },
    publishForm(){
      this.submitForm(1);
    },
    submitData(fileList) {
      this.form.images = fileList;
      if (this.form.consultationId != null) {
        updateContent(this.form).then(response => {
          this.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addContent(this.form).then(response => {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 提交按钮 */
    submitForm(isPushlish) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(isPushlish){
            this.form.publishStatusOpt = 1;
          }else{
            this.form.publishStatusOpt = 0;
          }
          this.$refs["upload"].doSubmit();
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const consultationIds = row.consultationId || this.ids;
      this.$confirm('是否确认删除信息咨询编号为"' + consultationIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delContent(consultationIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有信息咨询数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportContent(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
